package com.haitaoit.chengquan.module.home.activity.maintenance.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ListView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.utils.DateTimeUtil;
import com.haitaoit.chengquan.view.PullToRefreshBase;
import com.haitaoit.chengquan.view.PullToRefreshListView;

/**
 * Created by ljx on 2016/10/25.
 */

public class ViewPagerAdapter extends PagerAdapter {
    private Context context;
    private MaintenanceEvaAdapter maintenanceEvaAdapter;
    private MaintenanceServerAdapter maintenanceServerAdapter;

    public ViewPagerAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        View layout = null;
        if (position <= 1) {
            layout = LayoutInflater.from(context).inflate(R.layout.webview, null);
            ((WebView) layout.findViewById(R.id.webview)).loadDataWithBaseURL("about:blank", "346546446464", "text/html", "utf-8", null);
        } else {
            layout = LayoutInflater.from(context).inflate(R.layout.pull_refresh_listview, null);
            PullToRefreshListView pull_to_refresh_listView = (PullToRefreshListView) layout.findViewById(R.id.pull_to_refresh_listView);
            pull_to_refresh_listView.setPullLoadEnabled(true);
            pull_to_refresh_listView.setPullRefreshEnabled(false);
            pull_to_refresh_listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                }
            });
            pull_to_refresh_listView.setLastUpdatedLabel(DateTimeUtil
                    .getFormatDateTime(DateTimeUtil.getCurrTimestamp()));
            ListView listView = pull_to_refresh_listView.getRefreshableView();
            if (position == 2) {
                maintenanceEvaAdapter = new MaintenanceEvaAdapter(context);
                listView.setAdapter(maintenanceEvaAdapter);
            } else {
                maintenanceServerAdapter = new MaintenanceServerAdapter(context);
                listView.setAdapter(maintenanceServerAdapter);
            }

        }
        if (layout != null) {
            ((ViewPager) collection).addView(layout);
        }

        return layout;
    }


}
